package tankgame5;

/**
 * 1 .当发射一颗子弹后，就相当于启动一个线程 4
 * 2 .Hero有子弹的对象，当按下J时，我们就启动射发射行为
 * （线程），让子弹不停的移动，形成一个射击的效果
 * 3 .我们MyPanel需要不停的重绘子弹，才能出现该效果.
 * 4 .当子弹移动到面板的边界时，就应该销毁（把启动的子弹的
 * 线程销毁）
 */
public class Shot implements Runnable {

    int x;
    int y;
    int direct;
    int speed = 5;
    boolean isLive = true;

    public Shot(int x, int y, int direct) {
        this.x = x;
        this.y = y;
        this.direct = direct;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            switch (direct) {
                case 0:
                    y -= speed;
                    break;
                case 1:
                    x += speed;
                    break;
                case 2:
                    y += speed;
                    break;
                case 3:
                    x -= speed;
                    break;
            }
            System.out.println("(" + x + "," + y + ")");
            if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750 && isLive)) {
                isLive = false;
                break;
            }
        }
    }
}
